<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分库分表主键生成策略</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 4rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider span {
            background: white;
            padding: 0 2rem;
            position: relative;
            font-weight: 300;
            color: #9ca3af;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        .pros-cons-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 2rem;
            margin-top: 1.5rem;
        }
        @media (max-width: 768px) {
            .pros-cons-grid {
                grid-template-columns: 1fr;
                gap: 1rem;
            }
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    分库分表主键生成策略
                </h1>
                <p class="text-xl md:text-2xl font-light opacity-90 max-w-3xl mx-auto">
                    在分布式数据库架构中，如何生成全局唯一、高性能的主键ID是系统设计的关键挑战
                </p>
                <div class="mt-10 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-blur-sm">
                        <i class="fas fa-database mr-2"></i>
                        <span>6种核心方案</span>
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-blur-sm">
                        <i class="fas fa-chart-line mr-2"></i>
                        <span>性能对比分析</span>
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-blur-sm">
                        <i class="fas fa-code-branch mr-2"></i>
                        <span>实战最佳实践</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-lg p-10">
                <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                    在分库分表之后，主键 ID 的生成和管理成为一个关键问题，因为传统的自增 ID 或基于单一数据库的生成方式在这种场景下已经不再适用。本文将深入探讨六种主流的分布式ID生成策略，帮助您选择最适合的方案。
                </p>
            </div>
        </div>
    </section>

    <!-- Visualization -->
    <section class="py-16 px-6 bg-white">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12 text-gradient">主键生成策略全景图</h2>
            <div class="mermaid">
                graph TD
                    A[分布式ID生成策略] --> B[UUID]
                    A --> C[数据库序列]
                    A --> D[Snowflake算法]
                    A --> E[号段模式]
                    A --> F[中间件方案]
                    A --> G[混合模式]
                    
                    B --> B1[完全随机]
                    B --> B2[无需依赖]
                    
                    C --> C1[数据库内置]
                    C --> C2[简单可靠]
                    
                    D --> D1[时间有序]
                    D --> D2[高性能]
                    
                    E --> E1[预分配]
                    E --> E2[批量生成]
                    
                    F --> F1[Redis]
                    F --> F2[Zookeeper]
                    
                    G --> G1[灵活组合]
                    G --> G2[业务定制]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#3b82f6,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#ef4444,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#ec4899,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <!-- UUID -->
            <div class="mb-16 card-hover bg-white rounded-2xl shadow-lg p-8">
                <div class="flex items-start">
                    <div class="feature-icon bg-yellow-100 text-yellow-600">
                        <i class="fas fa-fingerprint"></i>
                    </div>
                    <div class="flex-1 ml-6">
                        <h3 class="text-2xl font-bold mb-4">1. UUID</h3>
                        <p class="text-gray-600 mb-6">UUID 是一种全局唯一标识符，不依赖于数据库实现，通过算法生成。</p>
                        
                        <div class="pros-cons-grid">
                            <div class="bg-green-50 rounded-lg p-6">
                                <h4 class="font-semibold text-green-800 mb-3">
                                    <i class="fas fa-check-circle mr-2"></i>优点
                                </h4>
                                <ul class="space-y-2 text-gray-700">
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-green-600 mt-1 mr-2 text-sm"></i>
                                        <span>无需依赖数据库，完全在应用层生成</span>
                                    </li>
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-green-600 mt-1 mr-2 text-sm"></i>
                                        <span>分布式场景中不同节点都可以独立生成，不存在冲突</span>
                                    </li>
                                </ul>
                            </div>
                            <div class="bg-red-50 rounded-lg p-6">
                                <h4 class="font-semibold text-red-800 mb-3">
                                    <i class="fas fa-times-circle mr-2"></i>缺点
                                </h4>
                                <ul class="space-y-2 text-gray-700">
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-red-600 mt-1 mr-2 text-sm"></i>
                                        <span>UUID 较长（36个字符），占用更多存储空间</span>
                                    </li>
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-red-600 mt-1 mr-2 text-sm"></i>
                                        <span>无序性导致索引和查询性能较差</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 数据库序列 -->
            <div class="mb-16 card-hover bg-white rounded-2xl shadow-lg p-8">
                <div class="flex items-start">
                    <div class="feature-icon bg-green-100 text-green-600">
                        <i class="fas fa-sort-numeric-up"></i>
                    </div>
                    <div class="flex-1 ml-6">
                        <h3 class="text-2xl font-bold mb-4">2. 数据库序列</h3>
                        <p class="text-gray-600 mb-6">使用数据库内建的序列生成机制，每个分库独立维护一组序列。</p>
                        
                        <div class="pros-cons-grid">
                            <div class="bg-green-50 rounded-lg p-6">
                                <h4 class="font-semibold text-green-800 mb-3">
                                    <i class="fas fa-check-circle mr-2"></i>优点
                                </h4>
                                <ul class="space-y-2 text-gray-700">
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-green-600 mt-1 mr-2 text-sm"></i>
                                        <span>简单易用，数据库本身支持</span>
                                    </li>
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-green-600 mt-1 mr-2 text-sm"></i>
                                        <span>可以保证各分库内 ID 的有序性</span>
                                    </li>
                                </ul>
                            </div>
                            <div class="bg-red-50 rounded-lg p-6">
                                <h4 class="font-semibold text-red-800 mb-3">
                                    <i class="fas fa-times-circle mr-2"></i>缺点
                                </h4>
                                <ul class="space-y-2 text-gray-700">
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-red-600 mt-1 mr-2 text-sm"></i>
                                        <span>需要设置不同起始序列号或步长，管理复杂</span>
                                    </li>
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-red-600 mt-1 mr-2 text-sm"></i>
                                        <span>高并发环境下可能成为瓶颈</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Snowflake -->
            <div class="mb-16 card-hover bg-white rounded-2xl shadow-lg p-8">
                <div class="flex items-start">
                    <div class="feature-icon bg-blue-100 text-blue-600">
                        <i class="fas fa-snowflake"></i>
                    </div>
                    <div class="flex-1 ml-6">
                        <h3 class="text-2xl font-bold mb-4">3. Snowflake 算法</h3>
                        <p class="text-gray-600 mb-6">由 Twitter 开发的一种分布式 ID 生成算法，生成 64 位的唯一 ID。</p>
                        
                        <div class="pros-cons-grid">
                            <div class="bg-green-50 rounded-lg p-6">
                                <h4 class="font-semibold text-green-800 mb-3">
                                    <i class="fas fa-check-circle mr-2"></i>优点
                                </h4>
                                <ul class="space-y-2 text-gray-700">
                                    <li class="flex items-start">
                                        <i class="fas fa-chevron-right text-green-600 mt-1 mr-2 text-sm"></i>
                                        <span>高效生成全局唯一的、有序的 ID</span>
                                    </li>